package com.hx.mylombok.task.impl;

import com.hx.mylombok.task.AbstractFileTask;
import com.hx.mylombok.task.FileTask;

import javax.annotation.processing.ProcessingEnvironment;
import javax.tools.JavaFileObject;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
/**
 * @author guoxiaolin
 * @description
 * @since 2019/3/27 15:10
 */
public class FeignProxyTask  extends AbstractFileTask implements FileTask {
    public FeignProxyTask(String className) {
        super(className);
    }

    @Override
    public void generateFile(Map<String, String> setterMap, ProcessingEnvironment processingEnv) throws IOException {

        JavaFileObject builderFeignProxyFile = processingEnv.getFiler().
                createSourceFile(packageName + ".proxy.feign.IFeignProxyService" + sufixName);

        try (PrintWriter out = new PrintWriter(builderFeignProxyFile.openWriter())) {

            if (packageName != null) {
                out.print("package ");
                out.print(packageName);
                out.println(".proxy.feign;");
                out.println();
            }

            out.println("import com.hx.battery.common.hibernate.proxy.feign.IFeignProxyService;");
            out.println("import "+packageName+".domain."+sufixName+";");
            out.println("import "+packageName+".proxy.fallback.FeignProxyFallback"+sufixName+";");
            out.println("import org.springframework.cloud.openfeign.FeignClient;");

            out.println();
            out.println("@FeignClient(value = \"${spring.application.name}\",path = \"/proxy/"+sufixName+"\"," +
                    "fallback = FeignProxyFallback"+sufixName+".class)");
            out.println("public interface IFeignProxyService"+sufixName+" extends IFeignProxyService<"+sufixName+"> {");
            out.println();
//            if (setterMap.size() == 0){
//                setterMap.forEach((methodName, argumentType) -> {
//                    String args[] = argumentType.split(",");
//                    String returnType = args[0];
//                    int sumargs = args.length;
//                    out.println("    @Override");
//                    out.print("    public ApiResult " + methodName + " ");
//                    out.print("(");
//                    String argline = "";
//                    String primitive= "int,long,short,char,float,double,boolean,byte";
//                    for (int i = 1; i < sumargs; i++){
//                        if (primitive.indexOf(args[i]) != -1){//包含
//                            out.print(" @RequestParam(value = \"a" + i + "\" ) ");
//                        }else{
//                            out.print(" @RequestBody ");
//                        }
//                        if (i<sumargs-1){
//                            argline = argline + "a" + i + ", ";
//                            out.print(args[i] + " a" + i + ",");
//                        }else{
//                            argline = argline + "a" + i;
//                            out.print(args[i] + " a" + i);
//                        }
//                    }
//                    out.println(");");
//                });
//            }

            out.println();
            out.println("}");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            logger.info("FeignProxyTask of " + simpleClassName + " is over");
        }
    }
}
